<?php

class AdminRole extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model("admin_menu_model", "menu");
        $this->load->model("admin_power_model", "power");
        $this->load->model("admin_role_model", "role");
        $this->load->model("admin_user_model", "user");

        $this->load->service("admin_manage_service");
        $this->check = $this->admin_manage_service;
        $this->_adminInfo = $this->check->checkAdminLogin();

        $this->projectUrl = [
            "add" => MANAGE_URL . "&c=adminRole&m=add",
            "list" => MANAGE_URL . "&c=adminRole&m=lists",
            "edit" => MANAGE_URL . "&c=adminRole&m=edit",
            "doAdd" => MANAGE_URL . "&c=adminRole&m=doAdd",
            "doEdit" => MANAGE_URL . "&c=adminRole&m=doEdit",
            "del" => MANAGE_URL . "&c=adminRole&m=del",
        ];
    }

    /*
     * 
     */

    function lists() {

        try {

            #权限
            $this->check->checkPower(array("power" => "admin_role_view"));

            $data["navName"] = "角色列表";

            $page = $this->input->getInt("page", TRUE);
            if ($page <= 0) {
                $page = 1;
            }
            $pageCount = 20;
            $data["page"] = $page;
            $data["pageCount"] = $pageCount;

            $where = "where 1";
            $pageStrArr = array();

            //关键词
            $keyword = $this->input->getPostStr("keyword", TRUE);
            if ($keyword != "") {
                $pageStrArr["keyword"] = $keyword;
                $where .= " AND name LIKE '%" . $keyword . "%' or truename LIKE '%" . $keyword . "%'";
            }
            $data["keyword"] = $keyword;

            $type = $this->role->type();

            $listNum = $this->role->getDataCount($where);

            $is_ajax = $this->input->is_ajax_request();
            if ($is_ajax) {
                $dataList = $this->role->getData($where, $page, $pageCount);

                foreach ($dataList as $k => $v) {
                    $dataList[$k]["created_at_alias"] = date("Y-m-d H:i:s", $v["created_at"]);
                    $dataList[$k]["updated_at_alias"] = date("Y-m-d H:i:s", $v["updated_at"]);
                    $dataList[$k]["type_name"] = !empty($type[$v["type_id"]]) ? $type[$v["type_id"]]["name"] : "";
                }

                $data["dataList"] = $dataList;

                $result = [
                    "code" => 0,
                    "count" => $listNum,
                    "data" => $dataList,
                    "msg" => ""
                ];
                echo jsonEncode($result);
                exit;
            }

            #进行分页
            $pageStr = "";
            if (!empty($pageStrArr)) {
                foreach ($pageStrArr as $k => $v) {
                    $pageStr .= "&" . $k . "=" . $v;
                }
            }
            $data['base_url'] = $this->projectUrl["list"] . $pageStr;

            $data["listNum"] = $listNum;

            $this->layout->view('admin/adminRole/list', $data);
        } catch (Exception $e) {
            $msg = $e->getMessage();
            show_error($msg, 200, "系统提示");
        }
    }

    /*
     * 
     */

    function add() {

        try {

            #权限
            $this->check->checkPower(array("power" => "admin_role_add"));

            $data["navName"] = "添加角色";

            $adminMenu = $this->menu->getData("where status_id=1", 1, 0, "order by sort asc,id asc");
            $menu_data_tmp = [];
            foreach ($adminMenu as $k => $v) {
                $menu_data_tmp[$v["parent_id"]][$v["id"]] = [
                    "title" => $v["title"],
                    "id" => $v["id"],
                    "checked" => false,
                    "field" => "menu_id_arr[]",
                    "spread" => true,
                ];
            }
            $menu_data = [];
            $i = 0;
            foreach ($menu_data_tmp[0] as $k => $v) {
                $menu_data[$i] = $v;
                if (isset($menu_data_tmp[$k])) {
                    $j = 0;
                    foreach ($menu_data_tmp[$k] as $k2 => $v2) {
                        $menu_data[$i]["children"][$j] = $v2;
                        if (isset($menu_data_tmp[$k2])) {
                            $p = 0;
                            foreach ($menu_data_tmp[$k2] as $k3 => $v3) {
                                $menu_data[$i]["children"][$j]["children"][$p] = $v3;
                                $p++;
                            }
                        }
                        $j++;
                    }
                }
                $i++;
            }
            $data["menu_data"] = jsonEncode($menu_data);

            $adminPower = $this->power->getData("where status_id=1", 1, 0, "order by order_id asc,id asc");
            $power_data_tmp = [];
            foreach ($adminPower as $k => $v) {
                $power_data_tmp[$v["parent_id"]][$v["id"]] = [
                    "title" => $v["name"],
                    "id" => $v["action_name"],
                    "checked" => false,
                    "field" => "power_id_arr[]",
                    "spread" => true,
                    "action_name" => $v["action_name"],
                ];
            }
            $power_data = [];
            $i = 0;
            foreach ($power_data_tmp[0] as $k => $v) {
                $power_data[$i] = $v;
                if (isset($power_data_tmp[$k])) {
                    $j = 0;
                    foreach ($power_data_tmp[$k] as $k2 => $v2) {
                        $power_data[$i]["children"][$j] = $v2;
                        if (isset($power_data_tmp[$k2])) {
                            $p = 0;
                            foreach ($power_data_tmp[$k2] as $k3 => $v3) {
                                $power_data[$i]["children"][$j]["children"][$p] = $v3;
                                $p++;
                            }
                        }
                        $j++;
                    }
                }
                $i++;
            }
            $data["power_data"] = jsonEncode($power_data);

            $type = $this->role->type();
            $data["type"] = $type;

            $data["doAddUrl"] = $this->projectUrl["doAdd"];

            $this->layout->view('admin/adminRole/add', $data);
        } catch (Exception $e) {
            $msg = $e->getMessage();
            show_error($msg, 200, "系统提示");
        }
    }

    /*
     * 
     */

    function edit() {

        try {

            #权限
            $this->check->checkPower(array("power" => "admin_role_view"));

            $data["navName"] = "编辑角色";

            #编辑角色
            $editId = $this->input->getInt("id", TRUE);
            if ($editId <= 0) {
                throw new Exception("缺少id");
            }
            $editData = $this->role->getDataById($editId);
            if (empty($editData)) {
                throw new Exception("数据不存在");
            }

            $data["editData"] = $editData;
            $data["editId"] = $editId;

            $adminMenu = $this->menu->getData("where status_id=1", 1, 0, "order by sort asc,id asc");
            $menu_data_tmp = [];
            foreach ($adminMenu as $k => $v) {
                $menu_data_tmp[$v["parent_id"]][$v["id"]] = [
                    "title" => $v["title"],
                    "id" => $v["id"],
                    "field" => "menu_id_arr[]",
                    "spread" => true,
                ];
            }
            $menu_data = [];
            $i = 0;
            foreach ($menu_data_tmp[0] as $k => $v) {
                $menu_data[$i] = $v;
                $menu_data[$i]["checked"] = strrpos($editData["menu"], "|" . $v["id"] . "|") !== false ? true : false;
                if (isset($menu_data_tmp[$k])) {
                    $j = 0;
                    foreach ($menu_data_tmp[$k] as $k2 => $v2) {
                        $menu_data[$i]["children"][$j] = $v2;
                        $menu_data[$i]["children"][$j]["checked"] = strrpos($editData["menu"], "|" . $v2["id"] . "|") !== false ? true : false;
                        if ($menu_data[$i]["children"][$j]["checked"] == true) {
                            $menu_data[$i]["checked"] = false;
                        }
                        if (isset($menu_data_tmp[$k2])) {
                            $p = 0;
                            foreach ($menu_data_tmp[$k2] as $k3 => $v3) {
                                $menu_data[$i]["children"][$j]["children"][$p] = $v3;
                                $menu_data[$i]["children"][$j]["children"][$p]["checked"] = strrpos($editData["menu"], "|" . $v3["id"] . "|") !== false ? true : false;
                                if ($menu_data[$i]["children"][$j]["children"][$p]["checked"] == true) {
                                    $menu_data[$i]["children"][$j]["checked"] = false;
                                }
                                $p++;
                            }
                        }
                        $j++;
                    }
                }
                $i++;
            }
            $data["menu_data"] = jsonEncode($menu_data);

            $adminPower = $this->power->getData("where status_id=1", 1, 0, "order by order_id asc,id asc");
            $power_data_tmp = [];
            foreach ($adminPower as $k => $v) {
                $power_data_tmp[$v["parent_id"]][$v["id"]] = [
                    "title" => $v["name"],
                    "id" => $v["action_name"],
                    "field" => "power_id_arr[]",
                    "spread" => true,
                    "action_name" => $v["action_name"],
                ];
            }
            $power_data = [];
            $i = 0;
            foreach ($power_data_tmp[0] as $k => $v) {
                $power_data[$i] = $v;
                $power_data[$i]["checked"] = strrpos($editData["power"], "|" . $v["action_name"] . "|") !== false ? true : false;
                if (isset($power_data_tmp[$k])) {
                    $j = 0;
                    foreach ($power_data_tmp[$k] as $k2 => $v2) {
                        $power_data[$i]["children"][$j] = $v2;
                        $power_data[$i]["children"][$j]["checked"] = strrpos($editData["power"], "|" . $v2["action_name"] . "|") !== false ? true : false;
                        if ($power_data[$i]["children"][$j]["checked"] == true) {
                            $power_data[$i]["checked"] = false;
                        }
                        if (isset($power_data_tmp[$k2])) {
                            $p = 0;
                            foreach ($power_data_tmp[$k2] as $k3 => $v3) {
                                $power_data[$i]["children"][$j]["children"][$p] = $v3;
                                $power_data[$i]["children"][$j]["children"][$p]["checked"] = strrpos($editData["power"], "|" . $v3["action_name"] . "|") !== false ? true : false;
                                if ($power_data[$i]["children"][$j]["children"][$p]["checked"] == true) {
                                    $power_data[$i]["children"][$j]["checked"] = false;
                                }
                                $p++;
                            }
                        }
                        $j++;
                    }
                }
                $i++;
            }
            $data["power_data"] = jsonEncode($power_data);

            $type = $this->role->type();
            $data["type"] = $type;

            $data["doAddUrl"] = $this->projectUrl["doEdit"];

            $this->layout->view('admin/adminRole/add', $data);
        } catch (Exception $e) {
            $msg = $e->getMessage();
            show_error($msg, 200, "系统提示");
        }
    }

    /*
     * 
     */

    function doAdd() {

        #权限
        $flag = false;
        $msg = "操作失败";

        try {

            $this->check->checkPower(array("power" => "admin_role_add"));

            $name = $this->input->postStr("name", TRUE);
            $type_id = $this->input->postInt("type_id", TRUE);
            $power_id_arr = $this->input->postStr("power_id_arr", TRUE, false);
            $menu_id_arr = $this->input->postStr("menu_id_arr", TRUE, false);
            $remark = $this->input->postStr("remark", TRUE);

            $power = "";
            if (!empty($power_id_arr)) {
                $power = "|" . implode("|", $power_id_arr) . "|";
            }
            $menu = "";
            if (!empty($menu_id_arr)) {
                $menu = "|" . implode("|", $menu_id_arr) . "|";
            }

            if ($name == "") {
                throw new Exception("请输入角色名称");
            }
            $data = array(
                "name" => $name,
                "type_id" => $type_id,
                "power" => $power,
                "menu" => $menu,
                "remark" => $remark,
                "created_at" => time(),
                "updated_at" => time(),
            );
            $result = $this->role->addData($data);
            if ($result) {
                $flag = true;
                $msg = "操作成功";
            }
        } catch (Exception $e) {
            $msg = $e->getMessage();
        }

        $outArr = array("flag" => $flag, "msg" => $msg);
        echo json_encode($outArr);
    }

    /*
     * 
     */

    function doEdit() {

        #权限
        $this->check->checkPower(array("power" => "admin_role_edit"));

        $flag = false;
        $msg = "操作失败";

        try {
            $name = $this->input->postStr("name", TRUE);
            $type_id = $this->input->postInt("type_id", TRUE);
            $menu_id_arr = $this->input->postStr("menu_id_arr", TRUE, false);
            $power_id_arr = $this->input->postStr("power_id_arr", TRUE, false);
            $remark = $this->input->postStr("remark", TRUE);

            $editId = $this->input->postInt("id", TRUE);

            $oldData = $this->role->getDataById($editId);
            if (empty($oldData)) {
                throw new Exception("不存在的数据");
            }

            $power = "";
            if (!empty($power_id_arr)) {
                $power = "|" . implode("|", $power_id_arr) . "|";
            }
            $menu = "";
            if (!empty($menu_id_arr)) {
                $menu = "|" . implode("|", $menu_id_arr) . "|";
            }
            if ($name == "") {
                throw new Exception("请输入角色名称");
            }
            $paramArr = array(
                "fieldArr" => array(
                    "name" => $name,
                    "type_id"=>$type_id,
                    "power" => $power,
                    "menu" => $menu,
                    "remark" => $remark,
                    "updated_at" => time(),
                ),
                "whereArr" => array("id" => $editId),
            );
            $result = $this->role->updateData($paramArr);
            if ($result) {
                $flag = true;
                $msg = "操作成功";
            }
        } catch (Exception $e) {
            $msg = $e->getMessage();
        }

        $outArr = array("flag" => $flag, "msg" => $msg);
        echo jsonEncode($outArr);
    }

    /*
     * 删除记录
     */

    public function del() {

        #权限


        $flag = false;
        $msg = "操作失败";

        try {

            $this->check->checkPower(array("power" => "admin_role_del"));
            $delId = $this->input->postInt("id", TRUE);

            $oldData = $this->role->getDataById($delId);
            if (empty($oldData)) {
                throw new Exception("无数据");
            }

            $paramArr = array(
                "whereArr" => array(
                    "id" => $delId
                ),
            );
            $result = $this->role->delData($paramArr);
            if ($result) {
                $flag = true;
                $msg = "操作成功";
            }
        } catch (Exception $e) {
            $msg = $e->getMessage();
        }
        $outArr = array("flag" => $flag, "msg" => $msg);
        echo jsonEncode($outArr);
    }

}
